import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import { environment } from "../../../../../../environments/environment";
import { EntryHistoryService } from './entry-history.service';
import * as _ from "lodash";
import { Util } from '../../../../../common/util';
import { TablePageComponent } from '../../table/table-page/table-page.component';

@Component({
  selector: 'app-entry-history',
  templateUrl: './entry-history.component.html',
  styleUrls: ['./entry-history.component.scss'],
  providers: [EntryHistoryService]
})
export class EntryHistoryComponent implements OnInit {

  //查询条目历史列表
  @ViewChild("entryTablePage", { read: ViewContainerRef })
  entryTablePage: ViewContainerRef;
  tablePageComponent: ComponentRef<TablePageComponent>;
  //获取选中的数据的nNdId
  public nNjId: any;
  //全选按钮
  public isCheckedAll: any = false;
  public cloneList: any = [];
  //查询条件
  public getBody = {
    pageIndex: 0,
    pageSize: 0,
    nNjId: '',
    sOprDateStart: '',
    sOprDateEnd: ''
  }
  //条目历史列表
  public listData: any = [];
  constructor(
    public resolver: ComponentFactoryResolver,
    public entryHistoryService: EntryHistoryService
  ) { }

  ngOnInit() {
    // 初始化日期控件
    flatpickr.localize(zh);
    this.entryHistory();
   
  }
  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      // 更新为全选
      _.forEach(this.listData, item => {
        item.isChecked = true;
      });
    } else {
      // 更新为不全选
      _.forEach(this.listData, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.listData, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.listData.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  //查看条目历史
  entryHistory() {
    this.getBody.nNjId = this.nNjId;
    this.entryHistoryService.entryHistory(this.getBody).subscribe(data => {
      if (data) {
        this.listData = data.list;
        
        this.cloneList = _.cloneDeep(data.list);
        this.controlDom();
        this.showTablePage(this.listData)
        console.log(data);
      }
    })
  }
  //导出-查看条目历史
  exportExcel() {
    let that = this;
    // console.log("===============");
    // let checkedItem = [];
    // _.forEach(this.listData, item => {
    //   if (item.isChecked) {
    //     checkedItem.push(item.nQdId);
    //   }
    // })
    // nQdIdList=checkedItem;
    // let converItem: any = checkedItem.join(',');
    let getBody = {
      nNjId: this.nNjId,
      sOprDateStart: this.getBody.sOprDateStart,
      sOprDateEnd: this.getBody.sOprDateEnd
    }
    if (Util.dateOrder(that.getBody.sOprDateStart, that.getBody.sOprDateEnd)) {
      const iframe = window['$']("<iframe id='downloadiframe'>");
      iframe.attr('style', 'display:none');
      //  转码，兼容ie
      // getBody.sFullName = encodeURI(getBody.sFullName)
      iframe.attr('src', environment.server + '/pms/v1/riskmanage/data/export/itemHistory?token=' + sessionStorage.getItem('token') + '&nNjId=' + getBody.nNjId + '&sOprDateStart=' + getBody.sOprDateStart + '&sOprDateEnd=' + getBody.sOprDateEnd);
      window['$']('body').append(iframe);
      setTimeout("$('#downloadiframe').remove()", 60000);
    } else {
      window['swal']("提示", "开始日期 须早于 结束日期", "warning");
    }
  }
  //搜索查询
  searchQuery() {
    let that = this;
    if (Util.dateOrder(that.getBody.sOprDateStart, that.getBody.sOprDateEnd)) {
      that.entryHistory();
    } else {
      window['swal']("提示", "开始日期 须早于 结束日期", "warning");
    }
  }
  //根据编号是否相同操控dom
  controlDom() {
    let that = this;
    if (this.listData != null && this.listData.length > 0) {
      for (let i = 0; i < that.listData.length - 1; i++) {
        if (that.listData[i].nNjId == that.listData[i + 1].nNjId) {
          _.forIn(that.listData[i], (item1, key1) => {
            let index1 = i + 1;
            let index2 = i + 2;
            let id1 = "#route-table table tbody tr:nth-child(" + index2 + ") td div[id=" + key1 + "]";
            if (item1 == that.listData[i + 1][key1]) {
              // window['$'](id1).removeClass('font-color');
            } else {
              // window['$'](id1).addClass('font-color');
              that.cloneList[i][key1] = 'red';
            }
          })
        }
      }
    } else {
      this.controlDom();
    }
  }
   //查询历史变更记录列表
   showTablePage(item) {
    // 判断组件是否已创建，如果创建先注销
    if (this.tablePageComponent && this.tablePageComponent.instance) {
      this.tablePageComponent.destroy();
    }

    // 动态创建组件的实例
    const tablePageComponent = this.resolver.resolveComponentFactory(TablePageComponent);
    this.tablePageComponent = this.entryTablePage.createComponent(tablePageComponent);
    this.tablePageComponent.instance.listData = item;
    this.tablePageComponent.instance.listType = '5';
    this.tablePageComponent.instance.setColor = this.cloneList;
  }
}
